//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.whuims.pois.cluster;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.Cluster;
import org.whuims.pois.model.DataC;

import java.util.Iterator;
import java.util.List;

public class InsertCluster {
    BaseDao baseDao = new BaseDao();
    Log log = LogFactory.getLog(InsertCluster.class);
    public static final String YESTERDAY = "YESTERDAY";
    public static final String TODAY = "today";
    public static final String WEEK = "week";
    public static final String QUARTER = "quarter";
    final String category = "news";

    public InsertCluster() {
    }

    public void insertYesterday(List<DataC> list, long topicId) {
        List listDelete = this.ListToDelete("YESTERDAY", topicId);
        this.insert(list, "YESTERDAY", topicId);
        List<Cluster> listAfter = this.baseDao.findByParamsArr("from Cluster where topicId=? and type=?", topicId, YESTERDAY);
        if (listAfter.size() > listDelete.size())
            this.baseDao.deleteAll(listDelete);
    }

    public void insertWeek(List<DataC> list, long topicId) {
        List listDelete = this.ListToDelete("week", topicId);
        this.insert(list, "week", topicId);
        List<Cluster> listAfter = this.baseDao.findByParamsArr("from Cluster where topicId=? and type=?", topicId, WEEK);
        if (listAfter.size() > listDelete.size())
            this.baseDao.deleteAll(listDelete);
    }

    public void insertToday(List<DataC> list, long topicId) {
        List listDelete = this.ListToDelete("today", topicId);
        this.insert(list, "today", topicId);
        List<Cluster> listAfter = this.baseDao.findByParamsArr("from Cluster where topicId=? and type=?", topicId, TODAY);
        if (listAfter.size() > listDelete.size())
            this.baseDao.deleteAll(listDelete);
    }

    private List<Cluster> ListToDelete(String today, long topicId) {
        List list = this.baseDao.findByParamsArr("from Cluster where topicId=? and type=?", new Object[]{Long.valueOf(topicId), today});
        return list;
    }

    private void insert(List<DataC> list, String label, long topicId) {
        if(list != null) {
            for(int i = 0; i < list.size(); ++i) {
                DataC data = (DataC)list.get(i);
                Cluster cm = new Cluster();
                cm.setCategory("news");
                cm.setType(label);
                cm.setRecordid(data.getId());
                cm.setTopicId(topicId);
                StringBuilder builder = new StringBuilder();
                builder.append(data.getId());
                builder.append(",");
                int count = 1;
                int value = 1;

                for(Iterator similarID = data.getSimilarList().iterator(); similarID.hasNext(); ++value) {
                    DataC similar = (DataC)similarID.next();
                    builder.append(similar.getId());
                    builder.append(",");
                    ++count;
                }

                String var13 = builder.substring(0, builder.length() - 1);
                cm.setSimilarid(var13);
                cm.setCount(Integer.valueOf(count));
                cm.setValue(Double.valueOf((double)value));
                this.baseDao.create(cm);
            }

        }
    }

    private void truncate(String sql) {
        this.baseDao.excuteSql(sql);
    }
}
